package com.xing.crm.web;

import com.xing.crm.pojo.ProRole;
import com.xing.crm.pojo.ProShop;
import com.xing.crm.pojo.User;
import com.xing.crm.pojo.WebUser;
import com.xing.crm.service.ProRoleService;
import com.xing.crm.service.ProShopService;
import com.xing.crm.service.WebUserService;
import com.xing.crm.utils.MD5Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/** 展示端的页面控制器、 登录接口
 * @author Mr.L
 * @version V1.0
 * @date 2021年07月22日 11:20
 */
@Controller
@RequestMapping("/web")
public class WebsiteController {
	
	@Resource
	private WebUserService webUserService;
	@Resource
	private ProRoleService proRoleService;
	@Resource
	private ProShopService proShopService;
	@Value("${spring.mvc.static-path-pattern}")
	private String imgUriPrefix;
	
	/***************************************前端展示控制器*****************************************/
	@RequestMapping({"/index", "/"})
	public String web(HttpSession session, Model model) {
		String timestamp = String.valueOf(System.currentTimeMillis());
		WebUser user = (WebUser) session.getAttribute("webUser");
		model.addAttribute("shop", proShopService.getShopByUid(user));
		model.addAttribute("timstamp", timestamp);
		return "website/pages/category";
	}
	
	@RequestMapping("/product")
	public String products(Model model, String cid) {
		String timestamp = String.valueOf(System.currentTimeMillis());
		model.addAttribute("cid", cid);
		model.addAttribute("timstamp", timestamp);
		return "website/pages/product";
	}
	
	@RequestMapping("/product/info")
	public String productInfo(Model model, String id) {
		String timestamp = String.valueOf(System.currentTimeMillis());
		model.addAttribute("id", id);
		model.addAttribute("timstamp", timestamp);
		return "website/pages/productInfo";
	}
	
	
	/***************************************用户中心页控制器*****************************************/
	/** 功能描述: 用户中心 */
	@RequestMapping("/uc/index")
	public String ucIndex(Model model, HttpSession session) {
		/* 当前用户 */
		WebUser webUser = (WebUser) session.getAttribute("webUser");
		/* 所属店铺 */
		ProShop shop = proShopService.getShopByUid(webUser);
		model.addAttribute("shop", shop);
		return "website/uc/uc";
	}
	
	/** 功能描述: 商品管理 */
	@RequestMapping("/uc/shopProduct")
	public String shopProduct() {
		return "website/uc/shopProduct";
	}
	
	/** 功能描述: 店员管理 */
	@RequestMapping("/uc/shopClerk")
	public String shopClerk() {
		return "website/uc/shopClerk";
	}
	
	/** 功能描述: 入店申请 */
	@RequestMapping("/uc/shopApply")
	public String shopApply(Model model) {
		return "website/uc/shopApply";
	}
	
	
	
	/***************************************登录，校验*****************************************/
	/***************************************登录，校验*****************************************/
	/***************************************登录，校验*****************************************/
	
	@RequestMapping("/login")
	public String webLogin() {
		return "website/pages/login";
	}
	
	/**
	 * 功能描述: 登录

	 * @date 2021/8/11 10:53
	 * @param account
	 * @param password
	 * @param session
	 * @param model
	 * @return java.lang.String
	 * */
	@RequestMapping("/user/login")
	public String login(String account, String password, HttpSession session, Model model) {
		WebUser webUser = webUserService.login(account, password);
		User user = (User) session.getAttribute("user");
		//20210906 增加同一浏览器不能同时登录用户端和管理端，不知会不会和MyWebConfig中的登录判断起冲突 ？
		//后来老板又让给去掉了，我也不知会不会发神经，这已经触及到我的知识盲区了
		//if(user != null){
		//	model.addAttribute("msg", "当前浏览器已登录后台管理账号");
		//	return "website/pages/login";
		//}
		if(webUser != null) {
			ProRole role = proRoleService.getUserRole(webUser.getId());
			if(role != null) {
				session.setAttribute("isManager", "店长".equals(role.getName()));
				session.setAttribute("isClerk", "店员".equals(role.getName()));
			}
			session.setAttribute("role", role);
			session.setAttribute("webUser", webUser);
			return "redirect:/web/index";
		} else {
			model.addAttribute("msg", "账号或密码错误");
			return "website/pages/login";
		}
		
	}
	
	/**
	 * 功能描述: 登出

	 * @date 2021/8/11 10:53
	 * @param session
	 * @return java.lang.String
	 * */
	@RequestMapping("/user/logout")
	public String logout(HttpSession session) {
		/* 清除用户，权限信息 */
		session.removeAttribute("webUser");
		session.removeAttribute("role");
		session.removeAttribute("isManager");
		session.removeAttribute("isClerk");
		return "redirect:/web/login";
	}
	
	/**
	 * 功能描述: 验证密码
	 * @date 2021/8/11 10:52
	 * @param password
	 * @param session
	 * @return java.lang.Integer
	 * */
	@ResponseBody
	@RequestMapping("/checkedPwd")
	public Integer checkedPwd(String password, HttpSession session) {
		WebUser webUser = (WebUser) session.getAttribute("webUser");
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		Boolean isClerk = (Boolean) session.getAttribute("isClerk");
		/* 店员、店长 */
		boolean permit = (isManager != null && isManager) || (isClerk != null && isClerk);
		if(webUser != null && permit) {
			webUser.setPwd(MD5Util.stringMD5(password));
			return webUserService.checkedPwd(webUser);
		}
		return Integer.valueOf(0);
	}
	
	private String pathSub() {
		if(imgUriPrefix != null) {
			return imgUriPrefix.replace("/**", "");
		}
		return "";
	}
}
